<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName" @tab-click="() => null">
          <el-tab-pane label="用户管理" name="first">
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="dialogFormVisible = true"
              >
                添加角色
              </el-button>
            </el-row>
            <el-table
              :border="true"
              :data="tableData"
              style="width: 100%"
              :default-sort="{prop: 'date', order: 'descending'}"
            >
              <el-table-column
                type="index"
                label="序号"
                sortable
                width="120"
              />
              <el-table-column
                prop="name"
                label="角色名"
                sortable
                width="240"
              />
              <el-table-column
                prop="name"
                label="描述"
                sortable
              />
              <el-table-column
                prop="address"
                label="操作"
                width="250"
              >
                <template v-slot="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="handleGetRoleDetail(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="end">
              <el-pagination
                :page-size="page.pagesize"
                layout="total,prev,pager,next"
                :total="page.total"
                @current-change="changePage"
              />
            </el-row>
            <el-dialog width="800px" title="编辑角色" :visible.sync="dialogFormVisible" @close="cancel">
              <el-form ref="roleInfoForm" :rule="roleInfoRules" label-position="left" label-width="120px" :model="roleInfoData">
                <el-form-item label="角色名称">
                  <el-input v-model="roleInfoData.name" autocomplete="off" />
                </el-form-item>
                <el-form-item label="角色描述">
                  <el-input v-model="roleInfoData.description" autocomplete="off" />
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-row type="flex" :span="6" justify="center">
                  <el-button @click="dialogFormVisible = false">取 消</el-button>
                  <el-button type="primary" @click="submit">确 定</el-button>
                </el-row>
              </div>
            </el-dialog>
          </el-tab-pane>
          <el-tab-pane label="公司管理" name="second">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              :closable="true"
              description=""
              show-icon
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { addRole, deleteRole, getCompanyInfo, getRoleDetail, getRoleList, updateRole } from '@/api/settings'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      roleInfoData: {},
      roleInfoRules: {},
      dialogFormVisible: false,
      formLabelWidth: '50',
      formData: {},
      activeName: 'first',
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      }]
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getCompanyInfo()
    this.getRoleInfo()
  },
  methods: {
    cancel() {
      console.log('a')
      this.roleInfoData = {}
    },
    async handleGetRoleDetail(id) {
      this.roleInfoData = await getRoleDetail(id)
      this.dialogFormVisible = true
    },

    async submit() {
      try {
        await this.$refs.roleInfoForm.validate()
        //  处于编辑状态
        if (this.roleInfoData.id) {
          await updateRole(this.roleInfoData)
        } else {
          //  新增逻辑
          await addRole(this.roleInfoData)
        }

        this.$message.success('处理成功')
        this.dialogFormVisible = false
        this.getRoleInfo()
      } catch (e) {
        console.log(e)
      }
    },
    async delRole(id) {
      try {
        await deleteRole(id)
        this.$message.success('修改角色信息成功')
        this.getRoleInfo()
      } catch (e) {
        console.log(e)
      }
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    async getRoleInfo() {
      const { total, rows } = await getRoleList({
        page: this.page.page,
        pagesize: this.page.pagesize
      })
      this.tableData = rows
      this.page.total = total
    },
    changePage(page) {
      console.log(page)
      this.page.page = page
      this.getRoleInfo()
    }
  }
}
</script>

<style>

</style>

